package com.binguo.service;

import com.alibaba.fastjson.JSONObject;
import com.binguo.common.exception.ServiceException;
import com.binguo.model.mapper.YpCardMapper;
import com.binguo.model.model.*;
import com.binguo.model.repository.CustomerRechargeRepository;
import com.binguo.model.repository.CustomerRepository;
import com.binguo.model.repository.PayCardRepository;
import com.binguo.model.repository.YpCardRepository;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.binguo.common.exception.ErrorCode.*;

/**
 * @author yuritian
 * @Title: YpCardService
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2020-01-0914:39
 */
@Service
public class YpCardService {

    @Autowired
    ShopService shopService;

    @Autowired
    YpCardRepository ypCardRepository;

    @Resource
    YpCardMapper ypCardMapper;

    @Resource
    PayCardRepository payCardRepository;

    @Resource
    CustomerRepository customerRepository;

    @Resource
    CustomerService customerService;

    @Resource
    CustomerRechargeRepository rechargeRepository;

    public String getCardMenJinID(String cardID){
        String card_id = cardID;
        card_id = card_id.substring(0, 8);
        System.out.println(card_id);
        String card_id_1 = card_id.substring(0, 2);
        String card_id_2 = card_id.substring(2, 4);
        String card_id_3 = card_id.substring(4, 6);
        String card_id_4 = card_id.substring(6, 8);
        String newCard_id = card_id_4 + card_id_3 + card_id_2 + card_id_1;
        System.out.println(newCard_id);
        Long valueTen2 = Long.parseLong(newCard_id, 16);
        return valueTen2.toString();
    }

    public void registerYpCard(Integer shopId,
                               String cardId,
                               String cardNo){
        Condition condition = new Condition(YpCard.class);
        condition.or().andEqualTo("cardId", cardId)
                .andEqualTo("shopId", shopId)
                .andNotEqualTo("status",2);

        List<YpCard> ypCardList = ypCardRepository.findByCondition(condition);
        if (null == ypCardList || ypCardList.size() == 0) {
            condition.or().andEqualTo("cardNo", cardNo)
                    .andEqualTo("shopId", shopId)
                    .andNotEqualTo("status",2);
            ypCardList = ypCardRepository.findByCondition(condition);
            if (null == ypCardList || ypCardList.size() == 0) {
                YpCard ypCard = new YpCard();
                ypCard.setCardId(cardId);
                ypCard.setCardMenjinId(getCardMenJinID(cardId));
                ypCard.setCardNo(cardNo);
                ypCard.setShopId(shopId);
                ypCard.setStatus(0);
                ypCard.setCreateTime(new Date());
                ypCardRepository.save(ypCard);
            }else {
                throw new ServiceException(YPCARDNO_EXISTS);
            }
        } else {
            throw new ServiceException(YPCARD_EXISTS);
        }
    }

    public String getCardNo(String cardId,
                            Integer shopId){
        String cardNo;
        Condition condition = new Condition(YpCard.class);
        condition.or().andEqualTo("cardId", cardId)
                .andEqualTo("shopId", shopId);
        List<YpCard> ypCardList = ypCardRepository.findByCondition(condition);
        if (null == ypCardList || ypCardList.size() == 0) {
            throw new ServiceException(YPCARD_CHECK_ERROR);
        }
        cardNo = ypCardList.get(0).getCardNo();
        return cardNo;
    }

    public JSONObject getYpCardList(Integer shopId,
                                    Integer start){
        JSONObject result = new JSONObject();

        result.put("size",(ypCardMapper.getYpCardCount(shopId)/20)+1);
        result.put("ypCardList",ypCardMapper.getYpCardList(shopId,start,20));

        return result;
    }

    public JSONObject getYpCardMsg(String cardId,
                                   Integer shopId){
        JSONObject result = new JSONObject();
        Condition condition = new Condition(YpCard.class);
        condition.or().andEqualTo("cardId", cardId)
                .andEqualTo("shopId", shopId);
        List<YpCard> ypCardList = ypCardRepository.findByCondition(condition);
        if (null == ypCardList || ypCardList.size() == 0) {
            throw new ServiceException(WX_PAYCARD_NOT_EXIST);
        }
        String cardNo = ypCardList.get(0).getCardNo();

        Condition payCardCondition = new Condition(PayCard.class);
        payCardCondition.or().andEqualTo("cardno",cardNo)
                .andEqualTo("shopid",shopId);
        List<PayCard> payCardList = payCardRepository.findByCondition(payCardCondition);
        if (null == payCardList || payCardList.size() == 0) {
            throw new ServiceException(WX_PAYCARD_NOT_EXIST);
        }

        PayCard payCard = payCardList.get(0);

        Customer customer = customerRepository.findById(payCard.getCustomerid());

        Long accountbalance = payCard.getAccountbalance()+payCard.getBzAccountbalance()+payCard.getWxAccountbalance()+payCard.getYktAccountbalance();

        result.put("accountbalance",accountbalance);
        result.put("cardNo",cardNo);
        result.put("name",customer.getName());

        return result;
    }

    public void modifyYpCard(Integer shopId,
                             String cardId,
                             String cardNo){

        Condition condition = new Condition(YpCard.class);
        condition.or().andEqualTo("cardId", cardId)
                .andEqualTo("shopId", shopId)
                .andNotEqualTo("status", 2);

        List<YpCard> ypCardList = ypCardRepository.findByCondition(condition);
        if (null != ypCardList && ypCardList.size() > 0) {
            throw new ServiceException(YPCARD_EXISTS);
        }

        condition.or().andEqualTo("cardNo", cardNo)
                .andEqualTo("shopId", shopId)
                .andNotEqualTo("status", 2);
        ypCardList = ypCardRepository.findByCondition(condition);
        if (null != ypCardList && ypCardList.size() > 0) {
            throw new ServiceException(YPCARDNO_EXISTS);
        }
        YpCard ypCard = ypCardList.get(0);
        ypCard.setCardNo(cardNo);
        ypCardRepository.update(ypCard);
    }

    public void checkYpCard(Integer shopId,
                            String cardId,
                            String cardNo) {

        Condition condition = new Condition(YpCard.class);

        if (!StringUtil.isEmpty(cardId)){
            condition.or().andEqualTo("cardId", cardId)
                    .andEqualTo("shopId", shopId)
                    .andNotEqualTo("status", 2);

            List<YpCard> ypCardList = ypCardRepository.findByCondition(condition);
            if (null != ypCardList && ypCardList.size() > 0) {
                throw new ServiceException(YPCARD_EXISTS);
            }
        }
        if (!StringUtil.isEmpty(cardNo)){
            condition.or().andEqualTo("cardNo", cardNo)
                    .andEqualTo("shopId", shopId)
                    .andNotEqualTo("status", 2);
            List<YpCard> ypCardList = ypCardRepository.findByCondition(condition);
            if (null != ypCardList && ypCardList.size() > 0) {
                throw new ServiceException(YPCARDNO_EXISTS);
            }
        }

    }

    public void rechargeYpCard(String cardNo,
                               Integer shopId,
                               Long rechargeMoney){

        Condition payCardCondition = new Condition(PayCard.class);
        payCardCondition.or().andEqualTo("cardno",cardNo)
                .andEqualTo("shopid",shopId);
        List<PayCard> payCardList = payCardRepository.findByCondition(payCardCondition);


        if (null == payCardList || payCardList.size() == 0) {
            throw new ServiceException(WX_PAYCARD_NOT_EXIST);
        }

        PayCard payCard = payCardList.get(0);

        Long accountBalance = payCard.getAccountbalance();

        payCard.setAccountbalance(accountBalance+rechargeMoney);

        payCardRepository.update(payCard);

        Customer customer = customerRepository.findById(payCard.getCustomerid());
        CustomerExtend customerExtend = customerService.getCustomerExtend(payCard.getCustomerid(),payCard.getShopid());

        CustomerRecharge customerRecharge = new CustomerRecharge();
        BinguoOrder binguoOrder = new BinguoOrder();

        binguoOrder.setCardno(payCard.getCardno());
        binguoOrder.setCardid(payCard.getId());
        binguoOrder.setOrdertype(11);
        customerRecharge.setRechargeCustomerid(payCard.getCustomerid());
        customerRecharge.setRechargeMoney(rechargeMoney.intValue());
        customerRecharge.setRechargeDate(new Date());
        customerRecharge.setRechargeCustomerGroupid(customerExtend.getGroupId());
        customerRecharge.setRechargeShopid(shopId);
        customerRecharge.setRechargeCustomerName(customer.getName());
        customerRecharge.setRechargeCustomerPhone(customer.getPhone());
        customerRecharge.setRechargeCardAmount(accountBalance+payCard.getYktAccountbalance()+payCard.getWxAccountbalance()+payCard.getBzAccountbalance());
        customerRecharge.setRechargeDiscountMoney(0);
        customerRecharge.setRechargeType(61);
        rechargeRepository.save(customerRecharge);
        customerService.createRechargeOrder(customerRecharge,binguoOrder);
    }

    public Integer getYpCardCountByShopId(Integer shopId){
        return ypCardMapper.getYpCardCount(shopId);
    }
}
